<?php
namespace Api\Controller;
use Think\Controller;
class CommentController extends CommonController {
    public function _initialize(){
		
	}
	
	//评论状态接口
	public function commentcheck(){
		$id = I('get.id');
		
		$Comment = M('Ware_comment');
		$Shopcar = M('Ware_shopcar');
		$Cphoto	 = M('Comment_photo');
		
		$list = $Shopcar->where("user_id={$id} and status=5")->select();
		$data = array();
		if( $list == false ){
			$this->response( false , 1 , '没有数据' , true);
		}
		else{
			foreach( $list as $vo){
				$shopcar_id = $vo['id'];
				$replylist = array();
				$photo = array();
				$res = $Comment->where("shopcar_id={$shopcar_id}")->select();
				$ret = $Cphoto->where("shopcar_id={$shopcar_id}")->select();
				foreach( $res as $vo1){
					$replylist[] = array(
						'content'	=>	$vo1['content'],
						'good'		=>	$vo1['good'],
						'addtime'	=>	$vo1['addtime']
					);
				}
				foreach( $ret as $vo2){
					$photo[] = array(
						'img_src'	=>	$vo2['img_src'],
						'min_src'	=>	$vo2['min_src']
					);
				}
				$vo['reply'] = $replylist;
				$vo['photo'] = $photo;
				$data[] = $vo;
			}
			$this->response( true , 0 , $data , true);
		}
		
		
	}
	
	//提交评价内容
	public function commentbtn(){
		$content	= I('post.content');
		$good		= I('post.good');
		$ware_id	= I('post.ware_id');
		$order_id	= I('post.order_id');
		$id			= I('post.id');

		$Comment = M('Ware_comment');
		$Pay = M('Ware_pay');
		$Shopcar = M('Ware_shopcar');
		
		$res = $Comment->create();
		$ret = $Pay->where("order_id={$order_id}")->find();
		$ro  = $Shopcar->where("id={$id}")->find();
		
		$data = array();
		$status = array();//改变交易状态
		if( $res == false){
			$this->response( false , 1 , '评价失败' , true);
		}
		$data = array(
			'ware_id'		=>	$ware_id,
			'content'		=>	$content,
			'good'			=>	$good,
			'addtime'		=>	time(),
			'user_id'		=>	$_SESSION['uid'],
			'order_id'		=>	$order_id,
			'shopcar_id'	=>	$id
		);
		$status['status'] = 5;
		
		$res = $Comment->add($data);
		$ro  = $Shopcar->where("id={$id}")->save($status);
		$check = $Shopcar->where("order_id={$order_id} and status=0")->select();
		if( $check == false){
			$ret = $Pay->where("order_id={$order_id}")->save($status);
		}
		
		$this->response( true , 0 , '评价成功' , true);
	}
	
	//晒照片接口
	public function imgUpload(){
		$shopcar_id	= I('get.id');
		
		$upload = new \Think\Upload();// 实例化上传类
		$upload->maxSize   =     2*1024*1024 ;// 设置附件上传大小
		$upload->exts      =     array('jpg', 'gif', 'png', 'jpeg');// 设置附件上传类型
		$path = realpath(__ROOT__);
		$upload->rootPath  = $path.'/Public/uploadfiles/'; // 设置附件上传根目录
		$upload->savePath  =     ''; // 设置附件上传（子）目录
		// 上传文件 
		$info   =   $upload->upload();
		if(!$info) {// 上传错误提示错误信息
			$this->error($upload->getError());
		}else{// 上传成功
			$img_src="/Public/uploadfiles/{$info['_img']['savepath']}{$info['_img']['savename']}";
			$min_src="/Public/uploadfiles/{$info['_img']['savepath']}min_{$info['_img']['savename']}";
			$thumbpath="{$path}/Public/uploadfiles/{$info['_img']['savepath']}/";
			
			$image = new \Think\Image(); 
			$image->open("{$path}{$img_src}");
			$width = $image->width(); // 返回图片的宽度
			$height = $image->height(); // 返回图片的高度
			// 按照原图的比例生成一个最大为150*150的缩略图并保存为thumb.jpg
			$image->thumb($width, $height)->save($thumbpath.$info['_img']['savename']);
			$image->thumb(58, 58)->save($thumbpath.'min_'.$info['_img']['savename']);
			
			$res['shopcar_id']=$shopcar_id;
			$res['img_src']=$img_src;
			$res['min_src']=$min_src;
			$res['addtime']=time();
			
			$Cphoto	= M('Comment_photo');
			$Cphoto->add($res);
			
			echo $img_src;
		}				
	}
}
